package com.yipao.db.service.goods;

import com.github.pagehelper.PageHelper;
import com.yipao.db.dao.goods.GoodsMaterialMapper;
import com.yipao.db.domain.goods.GoodsMaterial;
import com.yipao.db.domain.goods.vo.GoodsMaterialReq;
import com.yipao.db.util.DeleteStatusConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 商品原料
 *
 * @author tianhao
 * @date 2020/5/10 13:49
 **/
@Service
public class GoodsMaterialService {
    @Resource
    private GoodsMaterialMapper goodsMaterialMapper;

    /**
     * 新增
     *
     * @param goodsMaterial
     * @return
     */
    public int insert(GoodsMaterial goodsMaterial) {
        goodsMaterial.setAddTime(LocalDateTime.now());
        goodsMaterial.setUpdateTime(goodsMaterial.getAddTime());
        goodsMaterial.setDeleted(DeleteStatusConstant.NOT_DELETED);
        return goodsMaterialMapper.insert(goodsMaterial);
    }

    /**
     * 修改
     *
     * @param goodsMaterial
     * @return
     */
    public int update(GoodsMaterial goodsMaterial) {
        goodsMaterial.setUpdateTime(LocalDateTime.now());
        return goodsMaterialMapper.update(goodsMaterial);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    public int delete(Integer id) {
        return goodsMaterialMapper.updateDeleteStatus(
                new GoodsMaterial(id, DeleteStatusConstant.IS_DELETED, LocalDateTime.now()));
    }


    /**
     * 分页,根据条件查询
     *
     * @param example
     * @return
     */
    public List<GoodsMaterial> selectByExample(GoodsMaterialReq example) {
        PageHelper.startPage(example.getPage(), example.getLimit());
        List<GoodsMaterial> goodsMaterials = goodsMaterialMapper.selectByExample(example);
        return goodsMaterials;

    }

    /**
     * 分页,根据条件查询
     *
     * @param id
     * @return
     */
    public GoodsMaterial queryById(Integer id) {
        return goodsMaterialMapper.selectByPrimaryKey(id, DeleteStatusConstant.NOT_DELETED);
    }

    /**
     * 查询全部
     *
     * @return
     */
    public List<GoodsMaterial> queryAll() {
        GoodsMaterialReq goodsMaterialReq = new GoodsMaterialReq();
        goodsMaterialReq.setDeleted(DeleteStatusConstant.NOT_DELETED);
        goodsMaterialReq.setOrd("desc");
        goodsMaterialReq.setSort("add_time");
        return goodsMaterialMapper.selectByExample(goodsMaterialReq);
    }
}
